package com.joelbitar.switchctrl;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

public class Preferences {
	private Context context;
	
	public static final String VIBRATE_LENGTH_KEY = "vibrateLength";
	public static final int VIBRATE_LENGTH = 0;
	public static final String REAL_REQUESTS_KEY = "sendRealRequests";
	public static final int REAL_REQUESTS = 1;

	public static final String GENERAL_PREFERENCES = "generalPreferences";
	
	private ArrayList<Preference> preferences = new ArrayList<Preference>(5);
	
	public Preferences(Context baseContext) {
		this.context = baseContext;
		this.initiatePreferences();
	}
	
	private void initiatePreferences()
	{
		ArrayList<Integer> vibrate_length_values = new ArrayList<Integer>();
		vibrate_length_values.add(50);
		vibrate_length_values.add(100);
		vibrate_length_values.add(200);
		vibrate_length_values.add(400);
		
		this.preferences.add(
				this.getPreference(
						VIBRATE_LENGTH,
						VIBRATE_LENGTH_KEY,
						Preference.TYPE_INT,
						100,
						R.string.settings_vibrate_length_name,
						R.string.settings_vibrate_length_desc,
						R.id.vibration_length_spinner,
						R.array.settings_vibrate_length_values,
						vibrate_length_values
				)
		);
		/*
		this.preferences.add(
				this.getPreference(
						REAL_REQUESTS,
						REAL_REQUESTS_KEY,
						Preference.TYPE_BOOL,
						1,
						R.string.settings_use_real_requests_name,
						R.string.settings_use_real_requests_desc,
						R.id.use_real_requests
				)
		);
		*/
	}

	private Preference getPreference(
			Integer id,
			String key,
			Integer type,
			Object defaultValue,
			Integer nameResource,
			Integer descResource,
			Integer resourceId,
			Integer SpinnerNamesResource,
			ArrayList<?> SpinnerValues)
	{
		// Gets the prereference with these parameters
		Preference p = this.getPreference(id, key, type, defaultValue, nameResource, descResource, resourceId);
		
		// adds the spinnernames thingys
		p.setSpinnerNamesResource(SpinnerNamesResource);
		p.setSpinnerValues(SpinnerValues);
		
		return p;
	}
	// Get the preference with the minimum amount of values
	private Preference getPreference(
			Integer id,
			String key,
			Integer type,
			Object defaultValue,
			Integer nameResource,
			Integer descResource,
			Integer resrouceId)
	{
		Preference p = this.getPreference(id, key, type, defaultValue, nameResource, descResource);
		p.setResourceId(resrouceId);
		
		return p;
	}
	
	// Get the preference with the minimum amount of values
	private Preference getPreference(
			Integer id,
			String key,
			Integer type,
			Object defaultValue,
			Integer nameResource,
			Integer descResource)
	{
		Preference p = new Preference(this.context);
		p.setId(id);
		p.setKey(key);
		p.setType(type);
		p.setDefault(defaultValue);
		p.setName(this.context.getString(nameResource));
		p.setDesc(this.context.getString(descResource));
		
		return p;
	}

	public Preference get(int preferenceId) {
		for(int i = 0; i < this.preferences.size(); i += 1)
		{
			Preference preference = this.preferences.get(i);
			if(preference.getId() == preferenceId)
			{
				return preference;
			}
		}
		return null;
	}
	
	public void save(Preference preference)
	{
		SharedPreferences prefs = this.context.getSharedPreferences(Preferences.GENERAL_PREFERENCES, Activity.MODE_PRIVATE);
		
		SharedPreferences.Editor editor = prefs.edit();

		if(preference.getType() == Preference.TYPE_INT || preference.getType() == Preference.TYPE_BOOL)
		{
			
			int value = (Integer) preference.getValue();
			
			Log.i("save", "Saving (Integer) value: " + value);
			
			editor.putInt(preference.getName(), value);
		}
		else if(preference.getType() == Preference.TYPE_STRING)
		{
			String value = (String) preference.getValue();

			Log.i("save", "Saving (string) value: " + value);
			
			editor.putString(preference.getName(), value);
		}
		
		editor.commit();
	}
	
	public Object getValue(Preference preference)
	{
		SharedPreferences prefs = this.context.getSharedPreferences(Preferences.GENERAL_PREFERENCES, Activity.MODE_WORLD_READABLE);
		
		Object value = null;
		
		if(preference.getType() == Preference.TYPE_INT || preference.getType() == Preference.TYPE_BOOL)
		{
			value = (Object) prefs.getInt(preference.getName(), (Integer) preference.getDefault());
		}
		else if(preference.getType() == Preference.TYPE_STRING)
		{
			value = (Object) prefs.getString(preference.getName(), (String) preference.getDefault());
		}
		
		return value;
	}
	
}
